package com.bjdd.jdy.ui.hotMap;


import android.graphics.Color;

import android.support.v4.app.Fragment;


import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Gradient;
import com.baidu.mapapi.map.HeatMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseFragment;
import com.bjdd.jdy.entity.HotMapBean;
import com.bjdd.jdy.util.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotMapFragment extends BaseFragment<HotMapPresenter, HotMapModel> implements HotMapContract.View {


    @Bind(R.id.map_hot)
    MapView mapHot;
    private BaiduMap mBaiduMap;
    private String cityName;
    private int day = 30;
    private int radius = 6;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot_map;
    }

    @Override
    public void initView() {
        mBaiduMap = mapHot.getMap();
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
    }

    @Override
    public void onResume() {
        mapHot.onResume();
        super.onResume();
    }


    @Override
    public void onDestroy() {
        mapHot.onDestroy();
        super.onDestroy();
    }

    @Override
    public void setCenter(BDLocation location) {
        L.d("setCenter");
        L.d("经纬度："+location.getLatitude() + "----"+location.getLongitude());
        cityName = location.getCity();
        presenter.getHotMap(cityName,day);
        radius = (int) location.getRadius();
        LatLng cenpt = new LatLng(location.getLatitude(),location.getLongitude());
        //定义地图状态
        MapStatus mapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(18)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mapStatusUpdate);
        BitmapDescriptor centMark = BitmapDescriptorFactory
                .fromResource(R.mipmap.location);
        //构建MarkerOption,用于在地图上添加marker
        OverlayOptions options = new MarkerOptions()
                .position(cenpt)
                .icon(centMark);
        //mBaiduMap.addOverlay(options);
        mBaiduMap.setMyLocationEnabled(true);
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                .direction(0)
                .latitude(location.getLatitude())
                .longitude(location.getLongitude())
                .build();
        mBaiduMap.setMyLocationData(locData);
        BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.location);
        MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode,true,mCurrentMarker);
        mBaiduMap.setMyLocationConfigeration(config);
    }

    @Override
    public void success(List<HotMapBean> data) {
        L.d("数据长度："+data.size());
        List<LatLng> randomList = new ArrayList<>();
        for (int i=0;i<data.size();i++){
            LatLng latLng = new LatLng(data.get(i).getConsignerLat(),data.get(i).getConsignerLng());
            randomList.add(latLng);
        }
        setData(randomList);
    }

    private void setData(List<LatLng> randomList){
        //设置渐变颜色值
        int[] DEFAULT_GRADIENT_COLORS = {Color.rgb(102, 225,  0), Color.rgb(255, 0, 0) };
        //设置渐变颜色起始值
        float[] DEFAULT_GRADIENT_START_POINTS = { 0.2f, 1f };
        //构造颜色渐变对象
        Gradient gradient = new Gradient(DEFAULT_GRADIENT_COLORS, DEFAULT_GRADIENT_START_POINTS);
        //数据源
        //在大量热力图数据情况下，build过程相对较慢，建议放在新建线程实现
        HeatMap heatmap = new HeatMap.Builder()
                .data(randomList)
                .gradient(gradient)
                .radius(radius)
                .build();
        //在地图上添加热力图
        mBaiduMap.addHeatMap(heatmap);
    }

}
